Ext.define('jwawa.erp.framework.JwawaSubmit', {
    extend:'Ext.form.action.Submit',
    alternateClassName: 'Jwawa.Action.Submit',
    alias: 'formaction.jwawaSubmit',
    type: 'submit',

      /**
     * @private
     * Performs the submit of the form data.
     */
    doSubmit: function() {
        var formEl,
            ajaxOptions = Ext.apply(this.createCallback(), {
                url: this.getUrl(),
                method: this.getMethod(),
                headers: this.headers
            });

        // For uploads we need to create an actual form that contains the file upload fields,
        // and pass that to the ajax call so it can do its iframe-based submit method.
        if (this.form.hasUpload()) {
            formEl = ajaxOptions.form = this.buildForm();
            ajaxOptions.isUpload = true;
        } else {
//            ajaxOptions.params = this.getParams();
            ajaxOptions.jsonData = this.getParams();
        }

//        Ext.Ajax.request(ajaxOptions);
        Jwawa.framework.JwawaConnection.request(ajaxOptions);

        if (formEl) {
            Ext.removeNode(formEl);
        }
    },

    /**
     * @private
     * Builds the full set of parameters from the field values plus any additional configured params.
     */
    getParams: function() {
        var nope = false,
            configParams = this.callParent(),
            fieldParams = this.form.getFieldValues(this.submitEmptyText !== nope);
        return Ext.apply({}, fieldParams, configParams);
    },

    /**
     * @private
     */
    handleResponse: function(response) {
        var form = this.form,
            errorReader = form.errorReader,
            rs, errors, i, len, records;
        if (errorReader) {
            rs = errorReader.read(response);
            records = rs.records;
            errors = [];
            if (records) {
                for(i = 0, len = records.length; i < len; i++) {
                    errors[i] = records[i].data;
                }
            }
            if (errors.length < 1) {
                errors = null;
            }
            return {
                success : rs.success,
                errors : errors
            };
        }
        return response;
    },

    processResponse : function(response){
        this.response = response;
        if (!response) {
            return true;
        }
        return (this.result = this.handleResponse(response));
    }
});

Ext.preg('jwawaSubmitPlugin',
    Ext.define('jwawa.erp.framework.JwawaSubmitPlugin', {
        alias: 'plugin.jwawaSubmitPlugin',
        init:function (obj) {
            var form = null;
            if (typeof obj.getForm() == "object") {
                form = obj.getForm();
            } else {
                form = obj;
            }
            form.submit = function (options) {
                obj.getForm().doAction('jwawaSubmit', options);
            };
        }
    })
);